package com.wy.tranFile.config;

import com.sun.xml.internal.bind.v2.TODO;
import com.wy.tranFile.bean.Constant;
import com.wy.tranFile.bean.FileMap;
import com.wy.tranFile.pojo.FileInfo;
import com.wy.tranFile.util.FileUtils;
import com.wy.tranFile.util.StringUtils;
import com.wy.tranFile.util.TimeUtil;
import org.apache.tomcat.util.bcel.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import sun.reflect.misc.FieldUtil;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * @author 吴宇~
 * @motto 我亦无他，唯手熟尔。
 * @edition jdk1.8
 * @reflection 0-1 喧闹任其喧闹，自由我自为之，我自风情万种，与世无争。
 * @date 2024/06/29/22:07
 * @target 心若有所向往，何惧道阻且长
 */
@Component
public class ProjectPrepareHot implements CommandLineRunner {
    @Autowired
    private FileMap fileMap;
    @Autowired
    private Constant constant;
    // 编译正则表达式以提高匹配效率
    private static final Pattern pattern = Pattern.compile("^[0-9a-fA-F]{32}$");


    @Override
    public void run(String... args) throws Exception {

        //在这这儿将map里面装上东西
        File rootFile = new File(constant.getAddress());
        if (!rootFile.exists()) {
            rootFile.mkdirs();
        } else {
            int i = deleteSuperfluousFileOrDir();
            System.out.println("删除：" + i + "个");
            Map<String, FileInfo> allFileInfo = FileUtils.getAllFileInfo(constant.getAddress());
            System.out.println("存在合法文件" + allFileInfo.size() + "个");
            fileMap.getFileMaps().putAll(allFileInfo);
        }
    }

    /**
     * 删除无效的文件
     *
     * @return
     */
    public int deleteSuperfluousFileOrDir() {
        int deleteCount = 0;
        File file1 = new File(constant.getAddress());
        for (File file : FileUtils.getCurrentDirAllDir(file1)) {
            if (!file.getName().equals(TimeUtil.getCurrentYear())) {
                if (FileUtils.deleteDirectory(file)) {
                    deleteCount++;
                    System.out.println("删除" + file.getAbsolutePath());
                }
            } else {
                for (File file2 : FileUtils.getCurrentDirAllDir(file)) {
                    if (!file2.getName().equals(TimeUtil.getCurrentMonth())) {
                        if (FileUtils.deleteDirectory(file2)) {
                            System.out.println("删除" + file2.getAbsolutePath());
                            deleteCount++;
                        }
                    } else {
                        for (File file3 : FileUtils.getCurrentDirAllDir(file2)) {
                            if (!file3.getName().equals(TimeUtil.getDayOfMonth())) {
                                if (FileUtils.deleteDirectory(file3)) {
                                    deleteCount++;
                                    System.out.println("删除" + file3.getAbsolutePath());
                                }
                            }
                        }
                        FileUtils.deleteCurrentDirFile(file);
                    }
                }
                FileUtils.deleteCurrentDirFile(file);
            }
        }
        FileUtils.deleteCurrentDirFile(file1);
        return deleteCount;
    }
}
